package org.cypress.rtpack.utils;


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class FotterUtils {
    private FotterUtils() {
        throw new AssertionError("Cannot instantiate utility class!");
    }

    // 静态工具方法
    public static boolean isEmpty(String str) {
        return str == null || str.trim().isEmpty();
    }

    public static boolean pingReachable(String host) {
        try {
            String command;
            String os = System.getProperty("os.name").toLowerCase();

            if (os.contains("win")) {
                command = "ping -n 3 " + host; // Windows
            } else {
                command = "ping -c 3 " + host; // Linux/Mac
            }
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("TTL=") || line.contains("ttl=")) {    //如果中途有有效echo reply，则返回true。反之3次ping完 line为null
                    return true;
                }
            }

            return false;
        } catch (Exception e) {
            return false;
        }
    }

}
